/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFDvd.java
 *
 * Created on 18/06/2011, 10:35:39
 */
package br.unesp.demac.bcc.soo.view.cadastro;

import br.unesp.demac.bcc.soo.bean.Dvd;
import br.unesp.demac.bcc.soo.bean.Modalidade;
import br.unesp.demac.bcc.soo.dao.DvdDAO;
import br.unesp.demac.bcc.soo.dao.DvdDAOImp;
import br.unesp.demac.bcc.soo.dao.ModalidadeDAO;
import br.unesp.demac.bcc.soo.dao.ModalidadeDAOImp;
import br.unesp.demac.bcc.soo.util.Dialog;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author flavioam
 */
public final class JIFDvd extends javax.swing.JInternalFrame {

    /** Creates new form JIFDvd */
    public JIFDvd() {
        initComponents();
        populaModalidades();
        btnCancela.setEnabled(false);
        btnDeletaDvd.setEnabled(false);
        btnEditaDvd.setEnabled(false);
        btnSalvaDvd.setEnabled(false);
        habilitaCampos(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtCod = new javax.swing.JTextField();
        btnProcurar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtClassificacao = new javax.swing.JTextField();
        txtGenero = new javax.swing.JTextField();
        comboModalidade = new javax.swing.JComboBox();
        btnNovoDvd = new javax.swing.JButton();
        btnEditaDvd = new javax.swing.JButton();
        btnDeletaDvd = new javax.swing.JButton();
        btnSalvaDvd = new javax.swing.JButton();
        btnCancela = new javax.swing.JButton();
        dateDataCadastro = new com.toedter.calendar.JDateChooser();

        setClosable(true);
        setTitle("Cadastro de Dvd's");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico16x16/dvd-green.png"))); // NOI18N

        jLabel1.setText("Cód. Dvd:");

        btnProcurar.setText("Procurar");
        btnProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarActionPerformed(evt);
            }
        });

        jLabel2.setText("Nome: ");

        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        jLabel3.setText("Classificação:");

        jLabel4.setText("Gênero:");

        jLabel5.setText("Data Cadastro:");

        jLabel6.setText("Modalidade:");

        btnNovoDvd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/addItem.png"))); // NOI18N
        btnNovoDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoDvdActionPerformed(evt);
            }
        });

        btnEditaDvd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/ediItem.png"))); // NOI18N
        btnEditaDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditaDvdActionPerformed(evt);
            }
        });

        btnDeletaDvd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/removeItem.png"))); // NOI18N
        btnDeletaDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletaDvdActionPerformed(evt);
            }
        });

        btnSalvaDvd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/save.png"))); // NOI18N
        btnSalvaDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvaDvdActionPerformed(evt);
            }
        });

        btnCancela.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/cancel.png"))); // NOI18N
        btnCancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelaActionPerformed(evt);
            }
        });

        dateDataCadastro.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnProcurar))
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtClassificacao, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comboModalidade, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dateDataCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnNovoDvd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditaDvd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeletaDvd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSalvaDvd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancela)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProcurar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtClassificacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(dateDataCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(comboModalidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovoDvd)
                    .addComponent(btnEditaDvd)
                    .addComponent(btnDeletaDvd)
                    .addComponent(btnSalvaDvd)
                    .addComponent(btnCancela))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoDvdActionPerformed
        // TODO add your handling code here:
        habilitaCampos(true);
        limpaCampos();
        txtCod.setEnabled(false);
        btnCancela.setEnabled(true);
        btnSalvaDvd.setEnabled(true);
        btnNovoDvd.setEnabled(false);
        btnProcurar.setEnabled(false);
        txtNome.requestFocus();
}//GEN-LAST:event_btnNovoDvdActionPerformed

    private void btnEditaDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditaDvdActionPerformed
        // TODO add your handling code here:
        habilitaCampos(true);
        txtCod.setEnabled(false);
        btnSalvaDvd.setEnabled(true);
        btnCancela.setEnabled(true);
        btnNovoDvd.setEnabled(false);
        btnEditaDvd.setEnabled(false);
        btnProcurar.setEnabled(false);

    }//GEN-LAST:event_btnEditaDvdActionPerformed

    private void btnDeletaDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletaDvdActionPerformed
        // TODO add your handling code here:

        try {

            Dvd d = new Dvd();
            d.setIdDvd(Integer.parseInt(txtCod.getText()));

            DvdDAO dDao = new DvdDAOImp();
            dDao.deletaDvd(d);

            Dialog.sucessoDialog("Dvd Deletado com Sucesso!");

            limpaCampos();
            habilitaCampos(false);
            btnCancela.setEnabled(false);
            btnDeletaDvd.setEnabled(false);
            btnEditaDvd.setEnabled(false);
            btnSalvaDvd.setEnabled(false);
            btnNovoDvd.setEnabled(true);

            txtCod.setEnabled(true);
            btnProcurar.setEnabled(true);

        } catch (Exception e) {

            Dialog.errorDialog(e.getMessage());
        }

    }//GEN-LAST:event_btnDeletaDvdActionPerformed

    private void btnSalvaDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvaDvdActionPerformed
        // TODO add your handling code here:

        try {

            if (txtCod.getText().equals("")) {

                //novo

                Modalidade m = (Modalidade) comboModalidade.getSelectedItem();

                Dvd d = new Dvd();
                d.setModalidade(m);
                d.setClassificacao(Integer.parseInt(txtClassificacao.getText()));
                d.setDataCadastro(new Date());
                d.setGenero(txtGenero.getText());
                d.setNome(txtNome.getText());

                DvdDAO dDao = new DvdDAOImp();
                int idDvd = dDao.salvaDvd(d);

                if (idDvd != -1) {
                    txtCod.setText(String.valueOf(idDvd));
                    habilitaCampos(false);
                    txtCod.setEnabled(true);
                    btnProcurar.setEnabled(true);
                    btnCancela.setEnabled(false);
                    btnDeletaDvd.setEnabled(false);
                    btnEditaDvd.setEnabled(false);
                    btnSalvaDvd.setEnabled(false);
                    btnNovoDvd.setEnabled(true);
                    dateDataCadastro.setDate(d.getDataCadastro());

                    Dialog.sucessoDialog("Dvd Salvo com Sucesso!");
                } else {
                    Dialog.errorDialog("Erro ao salvar o DVD");
                }



            } else {
                //edita

                //novo

                Modalidade m = (Modalidade) comboModalidade.getSelectedItem();

                Dvd d = new Dvd();
                d.setModalidade(m);
                d.setClassificacao(Integer.parseInt(txtClassificacao.getText()));
                d.setGenero(txtGenero.getText());
                d.setNome(txtNome.getText());
                d.setIdDvd(Integer.parseInt(txtCod.getText()));

                DvdDAO dDao = new DvdDAOImp();
                if (dDao.editaDvd(d)) {
                    habilitaCampos(false);
                    txtCod.setEnabled(true);
                    btnProcurar.setEnabled(true);
                    btnCancela.setEnabled(false);
                    btnDeletaDvd.setEnabled(false);
                    btnEditaDvd.setEnabled(false);
                    btnSalvaDvd.setEnabled(false);
                    btnNovoDvd.setEnabled(true);
                    Dialog.sucessoDialog("Dvd Editado com Sucesso!");
                } else {
                    Dialog.errorDialog("Erro ao editar Dvd.");
                }

            }

        } catch (Exception e) {

            Dialog.errorDialog(e.getMessage());
        }
    }//GEN-LAST:event_btnSalvaDvdActionPerformed

    private void btnCancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelaActionPerformed
        // TODO add your handling code here:
        habilitaCampos(false);
        txtCod.setEnabled(true);
        btnProcurar.setEnabled(true);
        btnCancela.setEnabled(false);
        btnDeletaDvd.setEnabled(false);
        btnEditaDvd.setEnabled(false);
        btnSalvaDvd.setEnabled(false);
        btnNovoDvd.setEnabled(true);
        limpaCampos();

    }//GEN-LAST:event_btnCancelaActionPerformed

    private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomeActionPerformed

    private void btnProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarActionPerformed
        // TODO add your handling code here:
        if (txtCod.getText().equals("")) {

            Dialog.errorDialog("Digite um código a ser procurado!");
        } else {
            try {
                DvdDAO dDao = new DvdDAOImp();

                Dvd d = dDao.selecionaDvdById(Integer.parseInt(txtCod.getText()));

                if (d != null) {

                    txtCod.setEnabled(false);
                    txtClassificacao.setText(String.valueOf(d.getClassificacao()));
                    txtGenero.setText(d.getGenero());
                    txtNome.setText(d.getNome());
                    comboModalidade.setSelectedItem(d.getModalidade());
                    dateDataCadastro.setDate(d.getDataCadastro());
                    btnEditaDvd.setEnabled(true);
                    btnDeletaDvd.setEnabled(true);
                    btnCancela.setEnabled(true);
                    btnNovoDvd.setEnabled(false);

                } else {

                    Dialog.errorDialog("Dvd não encontrado!");
                }
            } catch (Exception ex) {
                Dialog.errorDialog(ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnProcurarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancela;
    private javax.swing.JButton btnDeletaDvd;
    private javax.swing.JButton btnEditaDvd;
    private javax.swing.JButton btnNovoDvd;
    private javax.swing.JButton btnProcurar;
    private javax.swing.JButton btnSalvaDvd;
    private javax.swing.JComboBox comboModalidade;
    private com.toedter.calendar.JDateChooser dateDataCadastro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtClassificacao;
    private javax.swing.JTextField txtCod;
    private javax.swing.JTextField txtGenero;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

    public void populaModalidades() {
        try {
        ModalidadeDAO mDao = new ModalidadeDAOImp();
        ArrayList<Modalidade> l;

            l = mDao.selecionaTodasModalidades();


        if (l != null) {

            for (Modalidade m : l) {

                comboModalidade.addItem(m);
            }
        }        } catch (Exception ex) {
            Dialog.errorDialog(ex.getMessage());
        }
    }

    public void habilitaCampos(boolean habilitar) {

        txtClassificacao.setEnabled(habilitar);
        txtGenero.setEnabled(habilitar);
        txtNome.setEnabled(habilitar);
        comboModalidade.setEnabled(habilitar);
    }

    public void limpaCampos() {

        txtClassificacao.setText("");
        txtCod.setText("");
        txtGenero.setText("");
        txtNome.setText("");
        dateDataCadastro.setDate(null);
    }
}
